home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 7628 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.5 sun4m)
  2. MIME-Version: 1.0
  3. Subject: Re: Randomness
  4. References: <451.6681T677T994@summat.demon.co.uk>
  5. Content-Type: text/plain; charset=us-ascii
  6. Content-Transfer-Encoding: 7bit
  7. Path: imada.ou.dk!breese
  8. From: breese@imada.ou.dk (Bjorn Reese)
  9. Message-ID: <1996Apr19.105527.4477@imada.ou.dk>
  10. Sender: news@imada.ou.dk
  11. Nntp-Posting-Host: gounod.imada.ou.dk
  12. Organization: Dept. of Math. & Computer Science, Odense University, Denmark
  13. Date: Fri, 19 Apr 1996 10:55:27 GMT
  14. Newsgroups: comp.sys.amiga.programmer
  15.  
  16. Mike Dodd wrote:
  17. > "FastRand" :
  18. [...]
  19. > but this produces a rather poor sequence of values...Soooerh, any better
  20. > suggestions ?
  21.  
  22. Yes, FastRand does exceptionally bad when RandomSeed is less than
  23. $80000000.
  24.  
  25. Try this one. Sorry about the C. It should be very easily translated
  26. though. If you find InitRandom() too hard to translate, you can just
  27. just FastRand (with RandomSeed > $80000000) to fill in rndValues.
  28.  
  29. /* RNDSIZE must be an integral number of 2 (and at least 8) */
  30. #define RNDSIZE 64
  31. #define RNDMASK (RNDSIZE - 1)
  32. typedef unsigned long RND;
  33.  
  34. static short rnd1;
  35. static short rnd2;
  36. static RND rndValues[RNDSIZE];
  37.  
  38. inline RND Random(void)
  39. {
  40.   rnd1 = (rnd1 - 1) & RNDMASK;
  41.   rnd2 = (rnd2 - 1) & RNDMASK;
  42.   return (rndValues[rnd2] += rndValues[rnd1]);
  43. }
  44.  
  45. void InitRandom(int s)
  46. {
  47.   int i;
  48.  
  49.   for (i = 0; i < RNDSIZE; i++) {
  50.     rndValues[i] = (RND)s;
  51.     /* --- randqd1() from Numerical Recipes --- */
  52.     s = 0x0019660dL * s + 0x3c6ef35fL;
  53.   }
  54.   rnd1 = 0;
  55.   rnd2 = RNDSIZE/2 - 3;
  56.  
  57.   /* --- Remove initial errors of bad seeding (warn-up) --- */
  58.   for (i = 0; i < RNDSIZE; i++) Random();
  59. }
  60.  
  61. -- 
  62. Bjorn Reese                      Email: breese@imada.ou.dk
  63. Odense University, Denmark       URL:   http://www.imada.ou.dk/~breese
  64.  
  65. "It's getting late in the game to show any pride or shame" - Marillion
  66.